package cn.edu.cumt.ec.shop.controller;
import cn.edu.cumt.ec.shop.util.file.FileUploadUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 *
 * @author xxfeng
 */
@RestController
@RequestMapping("/common")
@Api(value = "通用接口",tags = "通用接口")
public class CommonController {
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);


    @Value("${domainName}")
    private String domainName;

    @Value("${upload.path}")
    private String uploadPath;


    /**
     * 通用上传请求
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
    })
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件接口", notes = "上传文件接口")
    public JSONObject uploadFile(MultipartFile file) throws Exception {
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(uploadPath, file);
        JSONObject json = new JSONObject();
        json.put("url", domainName+fileName);
        return json;
    }


}
